var app = getApp(), api = getApp().api, is_no_more = !1, is_loading = !1, p = 2;
import '../../utils/utils.js'
Page({
    data: {
        status: -1,
        order_list: [],
        show_no_data_tip: !1,
        hide: 1,
        qrcode: "",
        showProtocolModal: false,
        maxtime: "",
        isHiddenLoading: true,
        isHiddenToast: true,
        countDownDay: 0,
        countDownHour: 0,
        countDownMinute: 0,
        countDownSecond: 0,
        system_msg:''
        
    },
  
    onLoad: function(t) {
        getApp().page.onLoad(this, t);
        var e = this;
        is_loading = is_no_more = !1, p = 2, e.setData({
            options: t
        }), e.loadOrderList(t.status || -1), getCurrentPages().length < 2 && e.setData({
            show_index: !0
        });

        
     
    },
    loadOrderList: function(t) {
        null == t && (t = -1);
        var e = this;
        e.setData({
            status: t
        }), getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        });
        var a = {
            status: e.data.status
        };
        e.data.options;
        void 0 !== e.data.options.order_id && (a.order_id = e.data.options.order_id), getApp().request({
            url: getApp().api.order.list,
            data: a,
            success: function(t) {
                0 == t.code && (e.setData({
                    order_list: t.data.list,
                    pay_type_list: t.data.pay_type_list
                }), console.log(t.data.list),
                  
                 getApp().core.getStorageSync(getApp().const.ITEM) && getApp().core.removeStorageSync(getApp().const.ITEM));
                e.setData({
                    show_no_data_tip: 0 == e.data.order_list.length
                });

              
            },
            complete: function() {
                getApp().core.hideLoading();
              // console.log(e.data.order_list)
            }
        });
    },

  // 页面渲染完成后 调用
  onReady: function () {
    
        
  },
    onReachBottom: function() {
        var a = this;
        is_loading || is_no_more || (is_loading = !0, getApp().request({
            url: getApp().api.order.list,
            data: {
                status: a.data.status,
                page: p
            },
            success: function(t) {
                if (0 == t.code) {
                    var e = a.data.order_list.concat(t.data.list);
                    a.setData({
                        order_list: e,
                        pay_type_list: t.data.pay_type_list
                    }), 0 == t.data.list.length && (is_no_more = !0);
                }
                p++;
            },
            complete: function() {
                is_loading = !1;
            }
        }));
    },
    orderPay_1: function(e) {
        var a = this, t = a.data.pay_type_list;
        1 == t.length ? (getApp().core.showLoading({
            title: "正在提交",
            mask: !0
        }), 0 == t[0].payment && a.WechatPay(e), 3 == t[0].payment && a.BalancePay(e)) : getApp().core.showModal({
            title: "提示",
            content: "选择支付方式",
            cancelText: "余额支付",
            confirmText: "线上支付",
            success: function(t) {
                getApp().core.showLoading({
                    title: "正在提交",
                    mask: !0
                }), t.confirm ? a.WechatPay(e) : t.cancel && a.BalancePay(e);
            }
        });
    },
    WechatPay: function(t) {
        getApp().request({
            url: getApp().api.order.pay_data,
            data: {
                order_id: t.currentTarget.dataset.id,
                pay_type: "WECHAT_PAY"
            },
            complete: function() {
                getApp().core.hideLoading();
            },
            success: function(t) {
                0 == t.code && getApp().core.requestPayment({
                    _res: t,
                    timeStamp: t.data.timeStamp,
                    nonceStr: t.data.nonceStr,
                    package: t.data.package,
                    signType: t.data.signType,
                    paySign: t.data.paySign,
                    success: function(t) {},
                    fail: function(t) {},
                    complete: function(t) {
                        "requestPayment:fail" != t.errMsg && "requestPayment:fail cancel" != t.errMsg ? getApp().core.redirectTo({
                            url: "/pages/order/order?status=1"
                        }) : getApp().core.showModal({
                            title: "提示",
                            content: "订单尚未支付",
                            showCancel: !1,
                            confirmText: "确认",
                            success: function(t) {
                                t.confirm && getApp().core.redirectTo({
                                    url: "/pages/order/order?status=0"
                                });
                            }
                        });
                    }
                }), 1 == t.code && getApp().core.showToast({
                    title: t.msg,
                    image: "/images/icon-warning.png"
                });
            }
        });
    },
    BalancePay: function(t) {
        getApp().request({
            url: getApp().api.order.pay_data,
            data: {
                order_id: t.currentTarget.dataset.id,
                pay_type: "BALANCE_PAY"
            },
            complete: function() {
                getApp().core.hideLoading();
            },
            success: function(t) {
                0 == t.code && getApp().core.redirectTo({
                    url: "/pages/order/order?status=1"
                }), 1 == t.code && getApp().core.showModal({
                    title: "提示",
                    content: t.msg,
                    showCancel: !1
                });
            }
        });
    },
    // 点击退款
    orderRevoke: function(t) {
      var a = this;
      
      getApp().core.showLoading({
        title: "正在加载",
        mask: !0
      }),
      
      // 读取数据库设置的退款时间

        getApp().request({
          url: getApp().api.order.system_msg,
          data: {
            id: '',
          },
          success: function (system_msg) {
            a.system_msg = system_msg,
            0 == system_msg.code && system_msg.setData({
              system_msg: system_msg,
            });

            console.log("后台系统设置信息：")
            console.log(system_msg)
          },
        });


        getApp().request({
          url: getApp().api.order.detail,
          data: {
            order_id: t.currentTarget.dataset.id,
          },
          success: function (e) {
            0 == e.code && a.setData({
              order_detail: e.data,
            });
            getApp().core.hideLoading();

            console.log("数据库下单时间：" + e.data.addtime);
            console.log("系统设置的退款时间限制为："+a.system_msg[0].refund_time + '天')

            

            // 数据库下单时间
            var addtime = e.data.addtime;

            var format = addtime.replace(/-/g, '/')
            var date = Date.parse(new Date(format))

            var date = new Date(date);
            var Y = date.getFullYear() + '/';
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
            var D = date.getDate() + parseInt(a.system_msg[0].refund_time) + ' ';
            var h = date.getHours() + ':';
            var m = date.getMinutes() + ':';
            var s = date.getSeconds();
            var endtime = Y + M + D + h + m + s;
            console.log("可申请退款的时间：" + endtime);

            var date2 = new Date(endtime);    //结束时间

            var curtime = new Date().format("yyyy-MM-dd hh:mm:ss");
            var curtime = curtime.replace(/-/g, '/')
            console.log("当前的时间：" + curtime);


            var date3 = date2.getTime() - new Date();   //时间差的毫秒数           

            //------------------------------
            var subMinutes = Math.floor(date3 / (60 * 1000)) //获取总共的分钟差



            //计算出相差天数
            var days = Math.floor(date3 / (24 * 3600 * 1000))

            //计算出小时数

            var leave1 = date3 % (24 * 3600 * 1000)    //计算天数后剩余的毫秒数
            var hours = Math.floor(leave1 / (3600 * 1000))
            //计算相差分钟数
            var leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数
            var minutes = Math.floor(leave2 / (60 * 1000))
            //计算相差秒数
            var leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数
            var seconds = Math.round(leave3 / 1000)
            console.log(" 相差 " + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒")

            // 时间相减 e

            if (seconds < 0) {
              console.log("可以退款！");
              var o = this;
              getApp().core.showModal({
                title: "提示",
                content: "是否退款该订单？",
                cancelText: "否",
                confirmText: "是",
                success: function (e) {
                  if (e.cancel) return !0;
                  e.confirm && (getApp().core.showLoading({
                    title: "操作中"
                  }), getApp().request({
                    url: getApp().api.order.revoke,
                    data: {
                      order_id: t.currentTarget.dataset.id
                    },
                    success: function (e) {
                      getApp().core.hideLoading(),
                        getApp().core.showModal({
                          title: "提示",
                          content: e.msg,
                          showCancel: !1,
                          success: function (o) {
                            e.confirm && e.onLoad({
                              id: o.data.order.order_id
                            });
                          }
                        });
                    }
                  }));
                }
              });
            } else {
              wx.showModal({
                title: '提示',
                content: " 请 " + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒 后再来申请退款",
                showCancel: false,
                confirmText: "ok"
              })
            }

          },
          complete: function () {
            getApp().core.hideLoading();
          }
        });
        
    },

    orderConfirm: function(e) {
        var a = this;
        getApp().core.showModal({
            title: "提示",
            content: "是否确认已收到货？",
            cancelText: "否",
            confirmText: "是",
            success: function(t) {
                if (t.cancel) return !0;
                t.confirm && (getApp().core.showLoading({
                    title: "操作中"
                }), getApp().request({
                    url: getApp().api.order.confirm,
                    data: {
                        order_id: e.currentTarget.dataset.id
                    },
                    success: function(t) {
                        getApp().core.hideLoading(), getApp().core.showToast({
                            title: t.msg
                        }), 0 == t.code && a.loadOrderList(3);
                    }
                }));
            }
        });
    },
    // 核销码
    orderQrcode: function(t) {
        var e = this, a = e.data.order_list, o = t.target.dataset.index;
        getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), e.data.order_list[o].offline_qrcode ? (e.setData({
            hide: 0,
            qrcode: e.data.order_list[o].offline_qrcode
        }), getApp().core.hideLoading()) : getApp().request({
            url: getApp().api.order.get_qrcode,
            data: {
                order_no: a[o].order_no
            },
            success: function(t) {
                0 == t.code ? e.setData({
                    hide: 0,
                    qrcode: t.data.url
                }) : getApp().core.showModal({
                    title: "提示",
                    content: t.msg
                });
            },
            complete: function() {
                getApp().core.hideLoading();
            }
        });
    },
    hide: function(t) {
        this.setData({
            hide: 1
        });
    },
    onShow: function() {
        getApp().page.onShow(this);
    }
});